package com.atguigu.gmall.product.apiController;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.common.cache.GmallCache;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.model.*;
import com.atguigu.gmall.product.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * title:
 * author: Gyh
 * date:2023-04-21 18:09
 * Description:
 */
@RestController
@RequestMapping("/api/product")
public class ProductApiController {

    @Autowired
    private SkuManageService skuManageService;

    @Autowired
    private SpuManageService spuManageService;

    @Autowired
    private BaseCategoryViewService baseCategoryViewService;

    @Autowired
    private BaseAttrInfoService baseAttrInfoService;

    @Autowired
    private BaseTrademarkService baseTrademarkService;

    /**
     * 根据skuId获取SkuInfo
     * @param skuId
     * @return
     */
    @GetMapping("/inner/getSkuInfo/{skuId}")
    public SkuInfo getSkuInfo(@PathVariable("skuId") Long skuId){
        SkuInfo skuInfo = skuManageService.getSkuInfo(skuId);
        return skuInfo;
    }

    /**
     * 根据三级分类id获取分类信息
     * @param category3Id
     * @return
     */
    @GetMapping("/inner/getCategoryView/{category3Id}")
    @GmallCache(prefix = "categoryView:")
    public BaseCategoryView getCategoryView(@PathVariable("category3Id") Long category3Id){
        return baseCategoryViewService.getById(category3Id);
    }

    /**
     * 根据skuId 获取最新的商品价格
     * @param skuId
     * @return
     */
    @GetMapping("/inner/getSkuPrice/{skuId}")
    public BigDecimal getSkuPrice(@PathVariable("skuId") Long skuId){
       return skuManageService.getSkuPrice(skuId);
    }

    /**
     * 根据spuId 获取海报数据
     * @param spuId
     * @return
     */
    @GetMapping("/inner/findSpuPosterBySpuId/{spuId}")
    public List<SpuPoster> getSpuPosterBySpuId(@PathVariable("spuId") Long spuId){
        List<SpuPoster> spuPosterList = spuManageService.getSpuPosterBySpuId(spuId);
        return spuPosterList;
    }

    /**
     * 根据skuId 获取平台属性数据
     * @param skuId
     * @return
     */
    @GetMapping("/inner/getAttrList/{skuId}")
    public List<BaseAttrInfo> getAttrList(@PathVariable("skuId") Long skuId){
        List<BaseAttrInfo> attrInfoList = baseAttrInfoService.getAttrList(skuId);
        return attrInfoList;
    }

    /**
     * 根据spuId,skuId 获取销售属性数据
     * @param skuId
     * @param spuId
     * @return
     */
    @GetMapping("/inner/getSpuSaleAttrListCheckBySku/{skuId}/{spuId}")
    public List<SpuSaleAttr> getSpuSaleAttrListCheckBySku(@PathVariable("skuId") Long skuId,
                                                          @PathVariable("spuId") Long spuId){
        List<SpuSaleAttr> list = spuManageService.getSpuSaleAttrListCheckBySku(skuId,spuId);
        return list;
    }

    /**
     * 根据spuId 获取到销售属性值Id 与skuId 组成的数据集
     * @param spuId
     * @return
     */
    @GetMapping("/inner/getSkuValueIdsMap/{spuId}")
    public Map getSkuValueIdsMap(@PathVariable("spuId") Long spuId){
        Map<String,Object> map = spuManageService.getSkuValueIdsMap(spuId);
        return map;
    }

    /**
     * 获取首页分类数据
     *  查询所有分类列表：一级分类对象包含二级分类对象，二级分类对象中包含三级分类对象
     * @return
     */
    @GetMapping("/inner/getBaseCategoryList")
    public Result getBaseCategoryList(){
        List<JSONObject> list = baseCategoryViewService.getBaseCategoryList();
        return Result.ok(list);
    }

    /**
     * 根据品牌Id 获取品牌数据
     * @param tmId
     * @return
     */
    @GetMapping("/inner/getTrademark/{tmId}")
    public BaseTrademark getTrademarkById(@PathVariable("tmId") Long tmId){
        BaseTrademark trademark = baseTrademarkService.getById(tmId);
        return trademark;
    }

}
